<?php

/**
 * @author jrodriguez
 * @copyright 2013
 */
 require_once $rootPath.'Gazzetta.DAL/Repositories/BaseRepository.php';
 require_once $rootPath.'Gazzetta.Entities/RSSSource.php';
 
 
class RSSSourceRepository extends BaseRepository 
{
    public function __construct()
    {
        $this->tableName = 'rsssource';
    }
    
    public function formatOne($row)
    {
        $rsssource = new RSSSource();
        
        foreach ($row as $field => $value) 
        {
            if($field == 'Id')
            {
                $rsssource->setId($value);
            }
            else if($field == 'Link')
            {
                $rsssource->setLink($value);
            }
            else if($field == 'Title')
            {
                 $rsssource->setTitle($value);
            }
            else if($field == 'Description')
            {
                 $rsssource->setDescription($value);
            }
            else if($field == 'IdCategory')
            {
            	$rsssource->setIdCategory($value);
            }
            else if($field == 'Enabled')
            {
            	$rsssource->setEnabled($value);
            }
        }          
                 
        return $rsssource;
    }   
    protected function buildColumnsPartForInsert()
    {
        $sql = "`Link`,`Title`, `Description`, `IdCategory`, `Enabled`";
        return $sql;
    }
    protected function buildValuesPartForInsert($entity)
    {
        $sql = '\''.$entity->getLink(). '\',\''. $entity->getTitle() . '\',\''. $entity->getDescription() . '\',\''. $entity->getIdCategory() . '\',\''. $entity->getEnabled() .'\'';
       
        return $sql;
    }
    
    protected function buildSetSentence($entity)
    {
        $sql = ' Link = \''.$entity->getLink(). '\', Title = \''. $entity->getTitle() . '\', Description = \''. $entity->getDescription() . '\', IdCategory = \''. $entity->getIdCategory() .'\', Enabled = '. $entity->getEnabled() ;
       
        return $sql;
    }
    
    public function exists($link)
    {
    	$conn = parent::buildConnection();
    	$result = $conn->getResultSet('SELECT * FROM RSSSource
										WHERE Link = \'' . $link . '\'');
    	return $result->count() > 0;
    }

    public function getActiveRssSources()
    {
    	return $this->getWithWhere(" WHERE Enabled = 1");
    }
}

?>